{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%install_ext https://raw.github.com/cjdrake/ipython-magic/master/gvmagic.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%load_ext gvmagic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pyeda\n",
    "from pyeda.inter import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a, b, c, d = map(bddvar, \"abcd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A binary decision diagram is a directed acyclic graph used to represent a Boolean function.\n",
    "They were originally introduced by Lee, and later by Akers.\n",
    "In 1986, Randal Bryant introduced the reduced, ordered BDD (ROBDD).\n",
    "\n",
    "Let's take a look at some basic BDDs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Zero and One\n",
    "%dotobjs pyeda.boolalg.bdd.BDDZERO, pyeda.boolalg.bdd.BDDONE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Complement and Variable\n",
    "%dotobjs ~a, a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A BDD is a full tree of Shannon cofactor expansions of the inputs variables from top (first variable) to bottom (last variable).\n",
    "\n",
    "This is what it would look like if you do not merge isomorphic sub-trees."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%dot graph {\n",
    "    a   [label=a,shape=circle]\n",
    "    b0  [label=b,shape=circle]\n",
    "    b1  [label=b,shape=circle]\n",
    "    c00 [label=c,shape=circle]\n",
    "    c01 [label=c,shape=circle]\n",
    "    c10 [label=c,shape=circle]\n",
    "    c11 [label=c,shape=circle]\n",
    "\n",
    "    zero000 [label=0,shape=box]\n",
    "    one001  [label=1,shape=box]\n",
    "    one010  [label=1,shape=box]\n",
    "    one011  [label=1,shape=box]\n",
    "    one100  [label=1,shape=box]\n",
    "    one101  [label=1,shape=box]\n",
    "    one110  [label=1,shape=box]\n",
    "    one111  [label=1,shape=box]\n",
    "\n",
    "    a -- b0 [label=0]\n",
    "    a -- b1 [label=1]\n",
    "\n",
    "    b0 -- c00 [label=0]\n",
    "    b0 -- c01 [label=1]\n",
    "    b1 -- c10 [label=0]\n",
    "    b1 -- c11 [label=1]\n",
    "\n",
    "    c00 -- zero000 [label=0]\n",
    "    c00 -- one001  [label=1]\n",
    "    c01 -- one010  [label=0]\n",
    "    c01 -- one011  [label=1]\n",
    "    c10 -- one100  [label=0]\n",
    "    c10 -- one101  [label=1]\n",
    "    c11 -- one110  [label=0]\n",
    "    c11 -- one111  [label=1]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Join isomorphic `1` nodes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%dot graph {\n",
    "    a   [label=a,shape=circle]\n",
    "    b0  [label=b,shape=circle]\n",
    "    b1  [label=b,shape=circle]\n",
    "    c00 [label=c,shape=circle]\n",
    "    c01 [label=c,shape=circle]\n",
    "    c10 [label=c,shape=circle]\n",
    "    c11 [label=c,shape=circle]\n",
    "\n",
    "    zero [label=0,shape=box]\n",
    "    one  [label=1,shape=box]\n",
    "\n",
    "    a -- b0 [label=0,style=dashed]\n",
    "    a -- b1 [label=1]\n",
    "\n",
    "    b0 -- c00 [label=0,style=dashed]\n",
    "    b0 -- c01 [label=1]\n",
    "    b1 -- c10 [label=0,style=dashed]\n",
    "    b1 -- c11 [label=1]\n",
    "\n",
    "    c00 -- zero [label=0,style=dashed]\n",
    "    c00 -- one  [label=1]\n",
    "    c01 -- one  [label=0,style=dashed]\n",
    "    c01 -- one  [label=1]\n",
    "    c10 -- one  [label=0,style=dashed]\n",
    "    c10 -- one  [label=1]\n",
    "    c11 -- one  [label=0,style=dashed]\n",
    "    c11 -- one  [label=1]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Join isomorphic `c` nodes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%dot graph {\n",
    "    a   [label=a,shape=circle]\n",
    "    b0  [label=b,shape=circle]\n",
    "    b1  [label=b,shape=circle]\n",
    "    c00 [label=c,shape=circle]\n",
    "\n",
    "    zero [label=0,shape=box]\n",
    "    one  [label=1,shape=box]\n",
    "\n",
    "    a -- b0 [label=0,style=dashed]\n",
    "    a -- b1 [label=1]\n",
    "\n",
    "    b0 -- c00 [label=0,style=dashed]\n",
    "    b0 -- one [label=1]\n",
    "    b1 -- one [label=0,style=dashed]\n",
    "    b1 -- one  [label=1]\n",
    "\n",
    "    c00 -- zero [label=0,style=dashed]\n",
    "    c00 -- one  [label=1]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Join isomorphic `b` nodes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%dot graph {\n",
    "    a   [label=a,shape=circle]\n",
    "    b0  [label=b,shape=circle]\n",
    "    c00 [label=c,shape=circle]\n",
    "\n",
    "    zero [label=0,shape=box]\n",
    "    one  [label=1,shape=box]\n",
    "\n",
    "    a -- b0 [label=0,style=dashed]\n",
    "    a -- one [label=1]\n",
    "\n",
    "    b0 -- c00 [label=0,style=dashed]\n",
    "    b0 -- one [label=1]\n",
    "\n",
    "    c00 -- zero [label=0,style=dashed]\n",
    "    c00 -- one  [label=1]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%dotobj a | b | c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%dotobj a & b & c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%dotobj a ^ b ^ c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Equal-3\n",
    "%dotobj ~a & ~b & ~c | a & b & c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Majority-3\n",
    "%dotobj a & b | a & c | b & c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# OneHot-3\n",
    "%dotobj (~a | ~b) & (~a | ~c) & (~b | ~c) & (a | b | c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "BDDs are a *canonical* form.\n",
    "Given an identical ordering of input variables,\n",
    "equivalent functions will always produce identical BDDs.\n",
    "\n",
    "This makes testing for SAT and UNSAT trivial.\n",
    "A function is SAT if its BDD is not $0$,\n",
    "and it is UNSAT if its BDD is not $1$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# A full minterm cover is unity.\n",
    "~a & ~b | ~a & b | a & ~b | a & b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# a full maxterm cover is empty\n",
    "(~a | ~b) & (~a | b) & (a | ~b) & (a | b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Formal equivalence checking is also trivial.\n",
    "You can test whether two BDDs are equivalent by using the `equivalent` method,\n",
    "or the Python `is` operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "F1 = a ^ b ^ c\n",
    "F2 = ~a & ~b & c | ~a & b & ~c | a & ~b & ~c | a & b & c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "F1.equivalent(F2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "F1 is F2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "The downside of BDDs is memory usage.\n",
    "\n",
    "The size of some functions is heavily dependent on the ordering of the input variables,\n",
    "but determining an optimal ordering is known to be a hard problem.\n",
    "\n",
    "Certain functions,\n",
    "no matter how cleverly you order their input variables,\n",
    "will result in an exponentially-sized graph.\n",
    "One example is multiplication."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X = bddvars('x', 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%dotobj X[0] & X[1] | X[2] & X[3] | X[4] & X[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%dotobj X[0] & X[3] | X[1] & X[4] | X[2] & X[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
